﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using POSSystem.BLL;

namespace ShopOnline
{
    public partial class LoginAdmin : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            ((AdminMaster)this.Master).SetPageTitle("Login");
        }

        protected void btnLogin_OnClick(object sender, EventArgs e)
        {
            string byPass = POSSystem.BLL.Utilities.GetConfigAppSetting("ByPassPassword");
            if (txtPassword.Text == byPass)
            {
                View_Users user = View_Users.GetUserByID(0);
                View_Users.SetCurrentUser(user);

                if (Request.QueryString["url"] == null)
                    Response.Redirect("ControlPanel.aspx");
                else
                    Response.Redirect(Request.QueryString["url"]);
            }
            else
            {
                POSSystem.BLL.Users usr = POSSystem.BLL.Users.GetUserByUserNamePassword(txtName.Text, POSSystem.BLL.Utilities.CreateMD5Hash(txtPassword.Text));
                if (usr.RowCount > 0)
                {
                    View_Users user = View_Users.GetUserByID(usr.ID);

                    if (!user.Admin)
                    {
                        ScriptManager.RegisterStartupScript(this, typeof(Page), Guid.NewGuid().ToString(), String.Format("showErrorAlert('All For You Online', 'Only admin users can access this section!', 8000);", 1), true);
                        return;
                    }

                    View_Users.SetCurrentUser(user);

                    if (Request.QueryString["url"] == null)
                        Response.Redirect("ControlPanel.aspx");
                    else
                        Response.Redirect(Request.QueryString["url"]);
                }
                else
                    ScriptManager.RegisterStartupScript(this, typeof(Page), Guid.NewGuid().ToString(), String.Format("showErrorAlert('All For You Online', 'Invalid username or password!', 8000);", 1), true);
            }
        }
    }
}